﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoolCode.Search
{
    public class Highlighter : IHighlighter
    {
        public IFormatter Formatter { get; set; }
        public int DisplayContentLength { get; set; }

        public Highlighter()
        {
            DisplayContentLength = 100;
            Formatter = new HtmlFormatter();
        }

        public string GetBestFragment(string q, string originalText)
        {
            if (originalText.Length > DisplayContentLength)
            {
                int ind = originalText.IndexOf(q, StringComparison.CurrentCultureIgnoreCase);
                if (ind > 0)
                {
                    if (ind > DisplayContentLength / 2)
                        originalText = "..." + originalText.Substring(ind - DisplayContentLength / 2);
                }
                if (originalText.Length > DisplayContentLength)
                {
                    originalText = originalText.Substring(0, DisplayContentLength) + "...";
                }
            }
            int ind2 = originalText.IndexOf(q, StringComparison.CurrentCultureIgnoreCase);
            if (ind2 > 0)
            {
                string key = originalText.Substring(ind2, q.Length);
                string highlight = Formatter.HighlightTerm( key);
                originalText = originalText.Replace(key, highlight);
            }
            return originalText;
        }

        public void Highlight(string q, IPaginatedList<SearchResult> results)
        {
          //  string highlight = string.Format("<font color=\"#c60a00\">{0}</font>", q);
            results.ForEach(c =>
            {
                // c.Title = c.Title.Replace(q, highlight);
                if (c.Content.Length > DisplayContentLength)
                {
                    int ind = c.Content.IndexOf(q, StringComparison.CurrentCultureIgnoreCase);
                    if (ind > 0)
                    {
                        if (ind > DisplayContentLength / 2)
                            c.Content = "..." + c.Content.Substring(ind - DisplayContentLength / 2);
                    }
                    if (c.Content.Length > DisplayContentLength)
                    {
                        c.Content = c.Content.Substring(0, DisplayContentLength) + "...";
                    }
                }
                int ind2 = c.Content.IndexOf(q, StringComparison.CurrentCultureIgnoreCase);
                if (ind2 > 0)
                {
                   string key  =c.Content.Substring(ind2, q.Length);
                   string highlight = string.Format("<font color=\"#c60a00\">{0}</font>", key);
                   c.Content = c.Content.Replace(key, highlight);
                }
                //    c.Content = c.Content.Replace(q, highlight);
            });
        }
    }

    public class ProHighlighter : IHighlighter
    {
       public  IFormatter Formatter {get;set;}
        #region IHighlighter Members

        public int DisplayContentLength
        {
            get;
            set;
        }

        private SegmentEngine seg = null;

        public ProHighlighter(SegmentEngine seg )
        {
            DisplayContentLength = 100;
            Formatter = new HtmlFormatter("<font color=\"red\">", "</font>");
            this.seg = seg;
        }

        public string GetBestFragment(string q, string originalText)
        {
            var highlighter = new KTHighlighter(Formatter, seg);
            string highlight = highlighter.GetBestFragment(q, originalText);
            if (!string.IsNullOrEmpty(highlight))
            {
              return  highlight;
            }
            return originalText;
        }

        public void Highlight(string q, IPaginatedList<SearchResult> results)
        {
            var  highlighter = new KTHighlighter(Formatter, seg);
            highlighter.FragmentSize = this.DisplayContentLength;
             IHighlighter  simpleHighlighter = new Highlighter();
             simpleHighlighter.DisplayContentLength = this.DisplayContentLength;
             simpleHighlighter.Formatter = this.Formatter;
            results.ForEach(c =>
            {
                string highlight = highlighter.GetBestFragment(q, c.Title);
                if (!string.IsNullOrEmpty(highlight))
                {
                    c.Title = highlight;
                }
                else
                {
                   c.Title =  simpleHighlighter.GetBestFragment(q, c.Title);
                }
                highlight = highlighter.GetBestFragment(q, c.Content);
                if (!string.IsNullOrEmpty(highlight))
                {
                    c.Content = highlight;
                }
                else
                {
                    c.Content = simpleHighlighter.GetBestFragment(q, c.Content);
                }
            });

        }

        #endregion
    }
}
